<?php

namespace app\common\model;

use app\admin\model\jeek\shop\SkuGoods;
use think\Model;


class OrderEvaluate extends Model
{


    // 表名
    protected $name = 'order_evaluate';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
//        'status_text'
    ];


    public function getStatusList()
    {
        return ['hidden' => __('Status hidden'), 'normal' => __('Status normal')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function skuGoods()
    {
        return $this->belongsTo(SkuGoods::class, 'goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function order()
    {
        return $this->belongsTo(Order::class, 'order_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    /**
     * Account:评论
     * User: Ixiangang
     * Date: 2025/2/13
     * Time: 14:50
     */
    public function getListPage($where = [])
    {
        $row = $this->getTermWhere($where);
        $field = $this->getField();
        $order = ['oe.id' => 'desc'];
        $list = $row->alias('oe')
            ->join('user u', 'u.id = oe.user_id', 'left')
            ->join('jeek_shop_sku_goods g', 'g.id = oe.goods_id', 'left')
            ->field($field)
            ->order($order)
            ->paginate()
            ->each(function ($item) {
                return $item;
            });
        return $list;
    }


    public function getList($where = [], $limit = 5)
    {
        $row = $this->getTermWhere($where);
        $field = $this->getField();
        $order = ['oe.id' => 'desc'];
        $list = $row->alias('oe')
            ->join('user u', 'u.id = oe.user_id', 'left')
            ->join('jeek_shop_sku_goods g', 'g.id = oe.goods_id', 'left')
            ->field($field)
            ->order($order)
            ->limit($limit)
            ->select();
        return $list;
    }


    public function getInfo($where = [])
    {
        $row = $this->getTermWhere($where);
        $field = $this->getField();
        $info = $row->alias('oe')
            ->join('user u', 'u.id = oe.user_id', 'left')
            ->join('jeek_shop_sku_goods g', 'g.id = oe.goods_id', 'left')
            ->field($field)
            ->find();
        return $info;
    }


    public function getField()
    {
        return "oe.id,oe.user_id,oe.order_id,oe.goods_id,oe.content,oe.status,oe.status,oe.createtime,
        u.nickname,u.avatar,
        g.name as goods_namen,g.image as goods_image";
    }


    public function getTermWhere($where)
    {
        return self::where(function ($query) use ($where) {
            if (isset($where['user_id']) && $where['user_id']) {
                $query->where('oe.user_id', $where['user_id']);
            }
            if (isset($where['goods_id']) && $where['goods_id']) {
                $query->where('oe.goods_id', $where['goods_id']);
            }
            # 查询订单
            if (isset($where['order_id']) && $where['order_id']) {
                $query->where('oe.order_id', $where['order_id']);
            }
            $query->where('oe.status', "normal");
        });
    }

}
